<?php
@$sous=$_POST[sous];
// 添加mongodb数据库中
$mongo = include 'mongo.php';
if($sous){
	// 查询条件为sous
	$where = ['title'=>['$regex'=>$sous]];
}else{
	$where=[];
}
	$options = [
		'projection' => ['title'=>1,'cnt'=>1]
	];
	$skip = @$_GET['skip']?:0;
	// 查询对象
	$query = new MongoDB\Driver\Query($where, $options);
	// 执行查询  一定要循环
	$data = $mongo->executeQuery('php71.article', $query);
	$query1 = new MongoDB\Driver\Query($where);

$data1 = $mongo->executeQuery('php71.article', $query1);
$arr = [];
$page = ceil(count($data1->toArray()) /2);

for($i=1;$i<=$page;$i++){
	$arr[$i-1] = [];
}
?>

<link rel="stylesheet" href="//res.layui.com/layui/dist/css/layui.css" media="all">
<link rel="stylesheet" href="//res.layui.com/static/css/global.css" media="all">
<script src="//cdn.bootcss.com/jquery/1.12.3/jquery.min.js"></script>
<script src="//res.layui.com/layui/release/layer/dist/layer.js?v=3111"></script>

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>文件列表</title>
	<link rel="stylesheet" type="text/css" href="./static/css/bootstrap.min.css">
</head>
<body>
	<br>
	<div class="container">
		<form action="" method="POST">
			<input type="text" name="sous" id="">
			<input type="submit" value="搜索">
			<input type="submit" value="查看全部">
		</form>
		<a href="add.php">添加文章</a>
		<table class="table table-hover">
			<thead>
				<tr>
					<th>ID</th>
					<th>标题</th>
					<th>内容</th>
					<th>操作</th>
				</tr>
			</thead>
			<tbody>
				<?php foreach($data as $key => $item):  ?>
					<tr>
						<td><?php echo $key+1; ?></td>
						<td><?php echo $item->title ?></td>
						<td><?php echo $item->cnt; ?></td>
						<td>
							<a href="edit.php?title=<?php echo $item->title ?>">修改</a> | 
							<a onclick="delfun('delete.php?title=<?php echo $item->title ?>')">删除</a>
						</td>
					</tr>
				<?php endforeach; ?>
			</tbody>
		</table>
		<ul class="pagination">
			<li><a href="#">«</a></li>
			<?php foreach($arr as $key => $item):  ?>
			<li><a href="index.php?skip=<?php echo $key ?>"><?php echo $key+1 ?></a></li>
			<?php endforeach; ?>
			<li><a href="#">»</a></li>
		</ul>
	</div>
	<script src="./static/js/jquery.min.js"></script>
	<script src="./static/js/bootstrap.min.js"></script>

	<script>
		
	function delfun(url){

		// 发送ajax
		/*let ret = $.get(url,{},function(ret){
			console.log(ret);
		},'json')*/
		if(confirm('确定要删除吗？')){
				let ret = $.get(url,{}).then(ret=>{
				let obj = JSON.parse(ret);
				alert(obj.msg);
				// 强制刷新
				location.reload(true);
			});
		}else{
			return
		}
	}


	</script>
</body>
</html>